package com.mew.slots.core.game.kbwsj.service;

import com.alibaba.fastjson.JSON;
import com.mew.common.enm.SlotsWinType;
import com.mew.common.entity.game.WinFree;
import com.mew.slots.core.common.game.BaseGameService;
import com.mew.slots.core.common.game.vo.BasePlayMsgs;
import com.mew.slots.core.common.messge.response.SlotsPlayResponseMessage;
import com.mew.slots.core.common.vo.StrategyGameVO;
import com.mew.slots.core.game.kbwsj.message.KbwsjGameResVO;
import com.mew.slots.core.game.kbwsj.message.KbwsjPlayResponseMessage;
import com.mew.slots.core.game.kbwsj.roller.KbwsjXml;
import com.mew.slots.core.game.kbwsj.room.KbwsjPlayMsgs;
import com.mew.slots.core.game.kbwsj.room.KbwsjRoom;
import org.apache.commons.lang3.StringUtils;

public class KbwsjService extends BaseGameService {
    @Override
    public BasePlayMsgs startGame(StrategyGameVO player) {

        String json = winFree.getJson();
        KbwsjGameResVO kvo = new KbwsjGameResVO();
        if (StringUtils.isNotEmpty(json)){
            try {
                kvo = JSON.parseObject(json, KbwsjGameResVO.class);
            } catch (Exception e) {
                kvo = new KbwsjGameResVO();
                System.out.println(json);
                e.printStackTrace();
            }
        }

        KbwsjRoom room = new KbwsjRoom();
        ((KbwsjPlayMsgs)room.res).vampireSeat = kvo.seat;
        ((KbwsjPlayMsgs)room.res).coinCol = kvo.coins[player.getOtherMsg()[0]];
        ((KbwsjPlayMsgs)room.res).vampire = kvo.vampire;
        ((KbwsjPlayMsgs)room.res).betIdx = player.getOtherMsg()[0];
        ((KbwsjPlayMsgs)room.res).freeNum = winFree.getFreeCount()-winFree.getFreeNumber();
        ((KbwsjPlayMsgs)room.res).freeType = winFree.getFreeType();
        ((KbwsjPlayMsgs)room.res).task = kvo.getTask();
        room.init(player.getRollerId(), KbwsjXml.getInstance(),player.getWinType()== SlotsWinType.Free.code(),player.getInBetNumber(),player.getControlType());
        room.roundPlay();
        return room.getRes();
    }

    @Override
    public SlotsPlayResponseMessage myResp(WinFree winFree, BasePlayMsgs res) {
        KbwsjPlayResponseMessage message = new KbwsjPlayResponseMessage();
        message.bouns = res.bouns;
        message.bounsGold = res.bounswin;

        message.allSlots = ((KbwsjPlayMsgs)res).allSlots;


        String json = winFree.getJson();
        KbwsjGameResVO kvo = new KbwsjGameResVO();
        if (StringUtils.isNotEmpty(json)){
            try {
                kvo = JSON.parseObject(json, KbwsjGameResVO.class);
            } catch (Exception e) {
                kvo = new KbwsjGameResVO();
                System.out.println(json);
                e.printStackTrace();
            }
        }

        if (((KbwsjPlayMsgs) res).reStart){
            message.oldTask = new int[][]{{0,0},{0,0},{0,0,0},{0,0,0},{0,0,0,0},{0,0,0,0}};
        }else {
            message.oldTask = kvo.task.clone();
        }

        winFree.setFreeType(((KbwsjPlayMsgs) res).freeType);
        if (winFree.getFreeNumber()==1&&winFree.getFreeType()==1){
            //特殊清空状态
            winFree.setFreeType(0);
        }


        if (((KbwsjPlayMsgs) res).vampireAdd>0){
            int bounsrate = null == winFree.getBounsRate() ? 0 : winFree.getBounsRate();
            winFree.setBounsRate(bounsrate+1);
            int bounsNum = null == winFree.getBounsNumber() ? 0 : winFree.getBounsNumber();
            bounsNum+=res.getBet();
            winFree.setBounsNumber(bounsNum);
        }


        kvo.coins[((KbwsjPlayMsgs) res).betIdx] = ((KbwsjPlayMsgs) res).coinCol;
        kvo.vampire = ((KbwsjPlayMsgs) res).vampire;

        kvo.task = ((KbwsjPlayMsgs) res).task;
        kvo.seat = ((KbwsjPlayMsgs) res).vampireSeat;
        //  任务
        winFree.setJson(JSON.toJSONString(kvo));
        message.coins = JSON.toJSONString(kvo.coins);
        message.freeCount = winFree.getFreeCount();
        message.turntable = ((KbwsjPlayMsgs) res).turntable;
        message.freeType = winFree.getFreeType();
        message.isVampire = ((KbwsjPlayMsgs) res).isVampire;

        long betavg = 0;
        if (message.isVampire>0){
            int bounsrate = null == winFree.getBounsRate() ? 0 : winFree.getBounsRate();
            int bounsNum = null == winFree.getBounsNumber() ? 0 : winFree.getBounsNumber();
            betavg = bounsrate==0?0:bounsNum/bounsrate;
            winFree.setBounsRate(0);
            winFree.setBounsNumber(0);
        }


        message.vampire = ((KbwsjPlayMsgs) res).isVampire==1?100:((KbwsjPlayMsgs) res).vampire;
        message.vampireBox = ((KbwsjPlayMsgs) res).vampireBox;
        message.vampireWin = ((KbwsjPlayMsgs) res).vampireWin*betavg;
        message.betAvg = betavg*res.line;

        message.task = ((KbwsjPlayMsgs) res).task;
        message.seat = ((KbwsjPlayMsgs) res).vampireSeat;
        message.w2 = ((KbwsjPlayMsgs) res).w2;
        return message;
    }

    @Override
    public void myAfterResp(SlotsPlayResponseMessage message,StrategyGameVO player, BasePlayMsgs res) {
        //WinFree winFree = player.getWinFree();
        ((KbwsjPlayResponseMessage)message).freeCount = winFree.getFreeCount();

    }


}
